package org.wyn.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * �����ļ������࣬ �ṩ�����ļ��Ķ�ȡ
 * 
 * @author HQ01U8433
 * 
 */
public class PropertieUtil {
	private static Map<String, Properties> _map = new HashMap<String, Properties>();
	/**
	 * ��������ļ���ƻ�ȡ�����ļ�����
	 * 
	 * @param propsName
	 * @return
	 */
	public static Properties getProps(String propsName) {
		if(_map.get(propsName) != null) {
			return _map.get(propsName);
		}
		Properties props = new Properties();
		FileInputStream fis = null;
		try {
			File f_file = new File(
					URLDecoder.decode(PropertieUtil.class.getClassLoader()
							.getResource(propsName+".properties").getFile(), "utf-8"));
			fis = new FileInputStream(f_file);
			props.load(fis);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (fis != null) {
				try {
					fis.close();
				} catch (IOException e) {
				}
			}
		}
		_map.put(propsName, props);
		return props;
	}

	/**
	 * ��������ļ������Ե���ƣ���ȡ�ַ����Ե�ֵ
	 * 
	 * @param propsName
	 * @param propName
	 * @return
	 */
	public static String getStrProp(String propsName, String propName) {
		Properties props = getProps(propsName);
		if (props != null) {
			String propValue = props.getProperty(propName);
			if (propValue != null && !propValue.isEmpty()) {
				return propValue;
			}
		}
		return null;
	}

	/**
	 * ��������ļ������Ե���ƣ���ȡ�������Ե�ֵ
	 * 
	 * @param propsName
	 * @param propName
	 * @return
	 */
	public static int getIntProp(String propsName, String propName) {
		Properties props = getProps(propsName);
		if (props != null) {
			String propValue = props.getProperty(propName);
			if (propValue != null && !propValue.isEmpty()) {
				return Integer.parseInt(propValue);
			}
		}
		return 0;
	}

	public static float getFloatProp(String propsName, String propName) {
		Properties props = getProps(propsName);
		if (props != null) {
			String propValue = props.getProperty(propName);
			if (propValue != null && !propValue.isEmpty()) {
				return Float.parseFloat(propValue);
			}
		}
		return 0.0f;
	}
	
	public static long getLongProp(String propsName, String propName) {
		Properties props = getProps(propsName);
		if (props != null) {
			String propValue = props.getProperty(propName);
			if (propValue != null && !propValue.isEmpty()) {
				return Long.parseLong(propValue);
			}
		}
		return 0l;
	}
}
